<?php

require_once(APP.'libs'.DS.'system_properties.php');
class Util{

	function start_session_if_need(){/*{{{*/
		$no_session = 0;
		$list_no_session = array(
			'user/ajaxListLuckUsers',
			'ajaxListRecentApply',

			'product/checkCaptcha',
		);
		foreach ($list_no_session as $s) {
			if (strstr($_SERVER['REQUEST_URI'], $s)	    ) {
				$no_session = 1;
			}
		}
		if ($no_session == 0) {
		   session_start();
		}

		return $no_session;
	}/*}}}*/

	function minuteToHour($minute)
	{
		if($minute==0){
			return 0;
		}
		$hour=0;
		$hour=intval($minute/60);
		$minute=$minute%60;
		if($hour==0){
			return $minute."分钟";
		}
		if($minute==0){
			return $hour."小时";
		}
		$minute=$hour."小时".$minute."分钟";
		return $minute;
	}
	function secToHour($minute)
	{
		if($minute<=0){
			return '0秒';
		}
		$hour=0;
		$hour=intval($minute/60);
		$minute=$minute%60;
		if($hour==0){
			return $minute."秒";
		}
		if($minute==0){
			return $hour."分钟";
		}
		if ($hour>60){
			$minute=intval($hour/60)."小时";	
		}else {
			$minute=$hour."分钟";	
		}
		
		return $minute;
	}
	
	function getDate($time,$nows)
	{
		//$now=date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
		$now=date("Y-m-d",strtotime($nows));
		$day=date("Y-m-d",strtotime($time));
		$times=date('H:i',strtotime($time));
		$count=intval((strtotime($now)-strtotime($day))/(3600*24));
		switch($count){
			case 0:
				$time="今天:".$times;
				break;
			case 1:
				$time="昨天:".$times;
				break;
			case 2:
				$time="前天:".$times;
				break;
		}
		return $time;
	}
	function getLevelToExp($g_level)
	{
		if($g_level<=20){
			$exp=SystemProperties::$level_exp[$g_level+1];			
		}else{
			$exp=SystemProperties::$level_exp[21]+SystemProperties::$Wlj_level*($g_level-20);
		}
		return $exp;
	}
	function getExpToLevel($exp)
	{
		$g_level=0;
		for($i=2;$i<=21;$i++){
			$cos_exp=SystemProperties::$level_exp[$i];
			if($exp<=$cos_exp){
				$g_level=$i-1;
				return $g_level;
			}
		}
		$g_level=21+intval(($exp-SystemProperties::$level_exp[21])/SystemProperties::$Wlj_level);
		return $g_level;
	}
	
	function getAgeFromID($id) {
	    if (strlen($id) == 15) {
	        $y = '19' .substr($id, 6, 2);
	    } else if (strlen($id) == 18) {
	        $y = substr($id, 6, 4);
	    } else {
	        $y = date('Y');
	    }
	    return data('Y') - $y;
	}
	function make_pager($num, $perpage, $curpage, $mpurl, $phpurl=1, $prefix='',$page_key_str = '',$a_name = '')
	{
		$pre =  "";
		$maxpages = 999999999;
		$multipage = '';

		if($phpurl) {
			$mpurl .= '';
		} else {
			$urlarr = $mpurl;
		}
		if($num > $perpage) {
			$page = 16;
			$offset = 8;
			$realpages = @ceil($num / $perpage);
			$pages = $maxpages && $maxpages < $realpages ? $maxpages : $realpages;
			if($page > $pages) {
				$from = 1;
				$to = $pages;
			} else {
				$from = $curpage - $offset;
				$to = $curpage + $page - $offset - 1;
				if($from < 1) {
					$to = $curpage + 1 - $from;
					$from = 1;
					if(($to - $from) < $page && ($to - $from) < $pages) {
						$to = $page;
					}
				} elseif($to > $pages) {
					$from = $curpage - $pages + $to;
					$to = $pages;
					if(($to - $from) < $page && ($to - $from) < $pages) {
						$from = $pages - $page + 1;
					}
				}
			}

			if($phpurl) {
				$url = $mpurl.$page_key_str.'/1'.$a_name;
				$url2 = $mpurl.$page_key_str.'/'.($curpage - 1).$a_name;
			} else {
				$urlarr[$page_key_str] = 1;
				$url = geturl($prefix,arraytostring($urlarr)).$a_name;
				$urlarr[$page_key_str] = $curpage - 1;
				$url2 = geturl($prefix,arraytostring($urlarr)).$a_name;
			}


			$multipage = '<div class="pager">'.(($curpage > 1)? '<a class="pg" href="'.$url.'" target="_self">首页</a> | ' : '').($curpage > 1 ? '<a class="pg" href="'.$url2.'" target="_self">上一页</a>' : '');
			for($i = $from; $i <= $to; $i++) {
				if($phpurl) {
					$url = $mpurl.$page_key_str.'/'.$i.$a_name;
				} else {
					$urlarr[$page_key_str] = $i;
					if($urlarr[$page_key_str] == 1) unset($urlarr[$page_key_str]);
					$url = geturl($prefix,arraytostring($urlarr)).$a_name;
				}
				$multipage .= ($i == $curpage ? ' | <strong style="color:red">'.$i.'</strong> ' : ' | <a class="pg" href="'.$url.'">'.$i.'</a>').(($i==$to)?'':'');

			}

			if($phpurl) {
				$url = $mpurl.$page_key_str.'/'.($curpage + 1).$a_name;
				$url2 = $mpurl.$page_key_str.'/'.$pages.$a_name;
				$url_end = $mpurl.$page_key_str.'/'.($pages).$a_name;
			} else {
				$urlarr[$page_key_str] = $curpage + 1;
				if($urlarr[$page_key_str] == 1) unset($urlarr[$page_key_str]);
				$url = geturl($prefix,arraytostring($urlarr)).$a_name;
				$urlarr[$page_key_str] = $pages;
				if($urlarr[$page_key_str] == 1) unset($urlarr[$page_key_str]);
				$url2 = geturl($prefix,arraytostring($urlarr)).$a_name;
				$url_end = geturl($prefix,arraytostring($urlarr)).$a_name;
			}

			$multipage .= ($curpage < $pages ? ' | <a  class="pg" href="'.$url.'" target="_self">下一页</a>' : '');
			$multipage .= (($curpage < $pages )? ' | <a class="pg" href="'.$url_end.'" target="_self">末页</a>' : '');
			$multipage.='</div>';
		}

		return $pre.$multipage;
	}	
}
